package com.ruoyi.app.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.google.gson.JsonObject;
import com.ruoyi.app.domain.OperStaff;
import com.ruoyi.app.domain.Operticket;
import com.ruoyi.app.domain.OperticketStep;
import com.ruoyi.app.service.IOperStaffService;
import com.ruoyi.app.service.IOperticketService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.vo.MyComponent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Anonymous;
import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.*;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

@RestController
@RequestMapping("/app/oper/")
public class ApiOperticketController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(ApiOperticketController.class);
    @Autowired
    private MyComponent myComponent;
    @Value("${api.url}")
    private String urls;
    @Autowired
    private IOperticketService iOperticketService;

    @Autowired
    private IOperStaffService iOperStaffService;

    @Anonymous
    @GetMapping("/getOpertickets")
    @ResponseBody
    public AjaxResult getOpertickets(HttpServletRequest request) {

        String id = getMemberId(request);
        OperStaff operStaff = iOperStaffService.getOperStaffById(Long.parseLong(id));

        String state = request.getParameter("state");
        Operticket operticket = new Operticket();
        operticket.setState(Integer.parseInt(state));
        operticket.setGroupId(operStaff.getStaffGroupId());
        List<Operticket> memberWalletList = iOperticketService.selectOperticketList(operticket);

        return AjaxResult.success("成功",memberWalletList);
    }
    @Anonymous
    @GetMapping("/getOperticketSteps")
    @ResponseBody
    public AjaxResult getOperticketSteps(HttpServletRequest request) {


        String code = request.getParameter("code");
        String type = request.getParameter("type");

        OperticketStep tempMemberWallet = new OperticketStep();
        tempMemberWallet.setOperTicketCode(Integer.parseInt(code));
        tempMemberWallet.setTicketType(Integer.parseInt(type));
        List<OperticketStep> memberWalletList = iOperticketService.selectOperticketStepList(tempMemberWallet);


        return AjaxResult.success("成功",memberWalletList);
    }

    @Anonymous
    @GetMapping("/toOperticketStep")
    @ResponseBody
    public AjaxResult toOperticketStep(HttpServletRequest request) {


        String code = request.getParameter("stepId");
        String type = request.getParameter("type");

        OperticketStep operticketStep = iOperticketService.selectOperticketStepById(Long.parseLong(code));
        if(operticketStep == null){
            return AjaxResult.error("该步骤不存在");
        }
        String url = "";
        try{


            JSONObject jobj= new JSONObject();
            jobj.put("id",code);
            jobj.put("switchId",operticketStep.getEquipId());
            String urls = myComponent.getUrls();
            String result = HttpUtils.sendPostByJson(urls,jobj.toJSONString());
            if(!result.equals("")){


//            String result = "{'id':'"+code+"','value':'2'}";
                JSONObject jsonObject = JSONObject.parseObject(result);
                String resCode = jsonObject.getString("code");

                JSONArray data = jsonObject.getJSONArray("data");
                JSONObject obj = (JSONObject) data.get(0);
                String s = obj.getString("value");
                String id = obj.getString("id");
                Map<String,Object> map = new HashMap<String,Object>();
                if(id.equals(code)){
                    map.put("status",s);
                    return AjaxResult.success("成功",map);
                }else{
                    return AjaxResult.error("失败");
                }
            }else{
                return AjaxResult.error("失败");
            }
        }catch (Exception e){
            log.error("请求异常{}", e);
            return AjaxResult.error("失败");
        }



    }

    @Anonymous
    @PostMapping ("/submitOperticketSteps")
    @ResponseBody
    public AjaxResult submitOperticketSteps(@RequestBody String reqParam,HttpServletRequest request) {


        try {
            JSONObject reqJson = JSONObject.parseObject(reqParam);
            long id = Long.parseLong(reqJson.getString("stepId"));
            int operIdentifyType = Integer.parseInt(reqJson.getString("operIdentifyType"));
            String stepEquipValue = reqJson.getString("stepEquipValue");
            int stepStatus = Integer.parseInt(reqJson.getString("stepStatus"));


            OperticketStep operticketStep = new OperticketStep();
            operticketStep.setId(id);
            operticketStep.setStatus(stepStatus);
            operticketStep.setActEquipValue(stepEquipValue);
            operticketStep.setOperIdentifyType(operIdentifyType);
            operticketStep.setCompleteTime(DateUtils.getTime());
            int res = iOperticketService.updateOperEquipStep(operticketStep);

            return AjaxResult.success("成功");
        }catch (Exception ex){
            return AjaxResult.error("失败");
        }

    }
    @Anonymous
    @PostMapping ("/submitOperticketType")
    @ResponseBody
    public AjaxResult submitOperticketType(@RequestBody String reqParam,HttpServletRequest request) {


        try {
            JSONObject reqJson = JSONObject.parseObject(reqParam);
            long id = Long.parseLong(reqJson.getString("stepId"));

            String stepEquipValue = reqJson.getString("stepEquipValue");

            int actOperType = Integer.parseInt(reqJson.getString("actOperType"));

            OperticketStep operticketStep = new OperticketStep();
            operticketStep.setId(id);

            operticketStep.setActOperType(actOperType);
            operticketStep.setActEquipValue(stepEquipValue);

            int res = iOperticketService.updateOperEquipStep(operticketStep);

            return AjaxResult.success("成功");
        }catch (Exception ex){
            return AjaxResult.error("失败");
        }
    }
    @Anonymous
    @PostMapping ("/submitOperticketStaff")

    @ResponseBody
    public AjaxResult submitOperticketStaff(@RequestBody String reqParam,HttpServletRequest request) {


        try {
            JSONObject reqJson = JSONObject.parseObject(reqParam);
            long id = Long.parseLong(reqJson.getString("id"));
            long staffId = Long.parseLong(reqJson.getString("staffId"));
            long executeStaffId = Long.parseLong(reqJson.getString("executeStaffId"));

            Operticket operticket1 = new Operticket();
            operticket1.setId(id);
            List<Operticket>  operticketList = iOperticketService.selectOperticketList(operticket1);
            if(operticketList == null){
                return AjaxResult.error("任务不存在");
            }
            if(operticketList.get(0).getExecuteStaffId() != 0){
                return AjaxResult.error("任务已分配执行人");
            }
            Operticket operticket = new Operticket();
            operticket.setId(id);
            operticket.setStaffId(staffId);
            operticket.setExecuteStaffId(executeStaffId);
            operticket.setState(1);
            int res = iOperticketService.updateOperticket(operticket);

            return AjaxResult.success("成功");

//            JsonArray jsonArray = new JsonParser().parse(data).getAsJsonArray();
//            for (JsonElement josns : jsonArray
//            ) {
//                JsonObject jsonObject = josns.getAsJsonObject();
//                int id = jsonObject.get("stepId").getAsInt();
//                String stepEquipValue = jsonObject.get("stepEquipValue").getAsString();
//                int stepStatus =jsonObject.get("stepStatus").getAsInt();
//                int actOperType = jsonObject.get("actOperType").getAsInt();
//
//                OperticketStep operticketStep = new OperticketStep();
//                operticketStep.setId(id);
//                operticketStep.setStatus(stepStatus);
//                operticketStep.setActOperType(actOperType);
//                operticketStep.setActEquipValue(stepEquipValue);
//                iOperticketService.updateOperEquipStep(operticketStep);
//            }


        }catch (Exception ex){
            return AjaxResult.error("失败");
        }
    }
}
